//# sourceURL=http://uMapApp/Module/StakeNum/StakeNum
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../../UI/Layer",
    "../../Earth/ViewPort"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort
) {

    function StakeNum(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        _this.stakeNum = StakeNum.transform(_this["桩号"]);
        _this.position = new uMap.Position({
            longitude : Number(_this["经度"])
            , latitude: Number(_this["维度"])
            , height  : Number(_this["高度"])
        });

        _this.title = _this["桩号"] + "/" + _this["朝向"];
        _this.searchKey = _this["桩号"] + " " + _this["朝向"];
        _this.sortKey = _this.stakeNum.distance;

        _this.layer = new Layer({
            "app"     : _this.app,
            "viewport": new ViewPort({
                "center": _this.position,
                "hpr"   : {
                    "heading": 0,
                    "pitch"  : -45,
                    "range"  : 50
                }
            })
        });

        _this.marker = new uMap.Entity({
            objectOption: {
                position: _this.position,
                point   : new uMap.PointGraphic({
                    objectOption: {
                        color                     : ["#fff"]
                        , disableDepthTestDistance: 0
                    }
                }),
                label   : new uMap.LabelGraphic({
                    objectOption: {
                        text                      : _this.title
                        , verticalOrigin          : "BOTTOM"
                        , pixelOffset             : [0, -10]
                        , disableDepthTestDistance: Number.POSITIVE_INFINITY
                    }
                })
            }
        });
        _this.layer.dataSources.entities.add(_this.marker.object);
    }

    var _class = StakeNum;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"  : {
            value: function () {
                if (!this.isOpened) {
                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close" : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle": {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active": {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {
                        // _this.layer.active();
                        _this.marker.label.fillColor = new uMap.Color(["#ff0"]);
                        _this.marker.label.scale = 1.2;
                        _this.marker.point.color = new uMap.Color(["#ff0"]);
                        _this.trigger("active");
                    } else {
                        _this.marker.label.fillColor = new uMap.Color(["#fff"]);
                        _this.marker.label.scale = 1;
                        _this.marker.point.color = new uMap.Color(["#fff"]);
                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }

            }
        }

    });

    Object.defineProperties(_class, {
        "transform"        : {
            value: function (stakeNum) {
                var _this = this;

                var pattern = /(K|k)\d{1,}(\+\d{1,3})*/;
                if (pattern.test(stakeNum)) {
                    stakeNum = stakeNum.replace(/K|k/, "K");
                    return {
                        "no"      : Number(stakeNum.split("+")[0].split("K")[1]),
                        "extend"  : Number(stakeNum.split("+")[1] || 0),
                        "distance": Number(stakeNum.split("+")[0].split("K")[1]) * 1000 + Number(stakeNum.split("+")[1] || 0),
                        "str"     : stakeNum
                    };
                }

            }
        },
        "transformDistance": {
            value: function (distance) {
                var _this = this;

                distance = Number(distance);

                if (!isNaN(distance) && distance >= 0) {

                    distance = Math.round(distance);

                    var no = Math.floor(distance / 1000);
                    var extend = distance - no * 1000;
                    var extendStr = "";

                    if (extend !== 0) {
                        extendStr = "+" + String(extend + 1000).substr(1, 3);
                    }

                    var str = "K" + no + extendStr;

                    return {
                        "no"      : no,
                        "extend"  : extend,
                        "distance": distance,
                        "str"     : str
                    };
                }

            }
        }
    });

    return _class;

});